#include"process_pool.h"
int tran_file(int new_fd)
{
    //放前面以防文件不存在
    int fd=open(FILENAME,O_RDONLY);
    ERROR_CHECK(fd,-1,"open")
    int ret;
    train_t t;
    //get name
    t.data_len=strlen(FILENAME);
    strcpy(t.buf,FILENAME);
    send_n(new_fd,&t,4+t.data_len);
    //get data
    struct stat buf;
    ret=stat(FILENAME,&buf);
    ERROR_CHECK(ret,-1,"stat");
    ERROR_CHECK(ret,-1,"stat")
    t.data_len=sizeof(buf.st_size);
    memcpy(t.buf,&buf.st_size,t.data_len);
    ret=send_n(new_fd,&t,4+t.data_len);
    SELFFUNC_ERR_CHECK(ret,"send_n")
    //sendfile send data
    ret=sendfile(new_fd,fd,0,buf.st_size);
    ERROR_CHECK(ret,-1,"sendfile");
    close(new_fd);
}
int recv_n(int sfd,void *buf,int data_len)
{
    char *p=(char*)buf;
    int total=0,ret;
    while(total<data_len)
    {
        ret=recv(sfd,p+total,data_len-total,0);
        total+=ret;
    }
    return 0;
}
int send_n(int sfd,void* buf,int data_len)
{
    char *p=(char*)buf;
    int total=0,ret;
    while(total<data_len)
    {
        ret=send(sfd,p+total,data_len-total,0);
        if(ret==-1)
        {
            return -1;
        }
        total+=ret;
    }
    return 0;
}